<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ProjectsController
 *
 * @author Poh Nean
 */
class ProjectsController extends PsController
{
	public function moduleAccessRules() {
		return array(
			array('Propsage.Projects')
		);
	}

    public $layout = 'manage';

    public function actionIndex()
    {
        $this->render('index');
    }

	public function actionSpaceBookingType()
	{
		$data = array();
		
		$criteria = new CDbCriteria;
		$criteria->compare('t.agency_id', Yii::app()->user->model->agency_id);

		$page = new CPagination;
		$page->pageSize = isset($_GET['pagesize']) ? $_GET['pagesize'] : 20;

		$sort = new CSort;
		$sort->defaultOrder = 't.pos ASC';

		$dataProvider = new CActiveDataProvider('SpaceBookingType', array(
					'criteria' => $criteria,
					'sort' => $sort,
					'pagination' => $page
				));
		
		
		$data['dataProvider'] = $dataProvider;
		$this->render('spacebookingtype', $data);
	}
	
    public function actionAddproject()
    {
        $this->render('addproject');
    }
	
	public function actionAddSpaceBookingType()
	{
		$data = array();
		$spaceBookingType = new SpaceBookingType;
		
		if(isset($_POST['SpaceBookingType'])) {
			$spaceBookingType->attributes = $_POST['SpaceBookingType'];
			$spaceBookingType->agency_id = Yii::app()->user->model->agency_id;
			$spaceBookingType->pos = $spaceBookingType->getNextPos();
			$is_success = $spaceBookingType->save();
			
			if($is_success) {
				PsSystemMessage::add('Space Booking Type added.', 'success');
				$this->redirect(array('projects/spaceBookingType'));
			} else {
				PsSystemMessage::add('Fail to add Space Booking Type.', 'error');
			}
		}
		
		$data['spaceBookingType'] = $spaceBookingType;
		$this->render('addspacebookingtype', $data);
	}
	
	public function actionUpdateSpaceBookingType($id)
	{
		$data = array();
		$spaceBookingType = $this->loadSpaceBookingTypeModel($id);
		
		if(isset($_POST['SpaceBookingType'])) {
			$spaceBookingType->attributes = $_POST['SpaceBookingType'];
			$is_success = $spaceBookingType->save();
			
			if($is_success) {
				PsSystemMessage::add('Space Booking Type Updated.', 'success');
				$this->redirect(array('projects/spaceBookingType'));
			} else {
				PsSystemMessage::add('Fail to update Space Booking Type.', 'error');
			}
		}
		
		$data['spaceBookingType'] = $spaceBookingType;
		$this->render('updatespacebookingtype', $data);
	}
	
	public function actionDeleteSpaceBookingType($id)
	{
		
		$response = array();

		$spaceBookingType = $this->loadSpaceBookingTypeModel($id);

		if ($spaceBookingType->hasUsedInProjects()) {
			$response['result'] = 'fail';
			$response['errorMsg'] = 'Type cannot be deleted. There are projects using this space type.';
		} else {
			if ($spaceBookingType->delete()) {
				$response['result'] = 'success';
			}
		}

		print(json_encode($response));
	}
	
	public function loadSpaceBookingTypeModel($id)
	{
		$model = SpaceBookingType::model()->ofCurrentAgency()->findByPk($id);
		
		return $model;
	}

}

?>
